      subroutine wrtgcrg(gcrgnam,gcrglen,icount1b,gchrgp,gchrg,nmedia,
     &                   scale,medeps,epkt)
c
      dimension gchrg(icount1b)
      integer gchrgp(3,icount1b),gcrglen,frmnum
      character*80 gcrgnam
c b+++++++++++++++++++++++
      real medeps(0:nmedia),epkt
c e++++++++++++++++++++++    
c
c     default gcrgnam is crg.dat
      open(20,file=gcrgnam(:gcrglen))
      frmnum=1
      write(20,*) "DELPHI OUTPUT FILE: GRID CHARGE"
      write(20,*) "FORMAT NUMBER=",frmnum
      write(20,*) "NUMBER OF CHARGES=",icount1b
c b+++++++++++++++
      do i = 0,nmedia
c        write(20,*)'DIELECTRIC IN MEDIUM NUMBER ',i,' :',medeps(i)*epkt
         write(20,*)'DIELECTRIC IN MEDIUM NUMBER ',i,' :',
     &               medeps(i),epkt,medeps(i)*epkt
                     
c        normally, there are at least two media: nmedia = 2
c        0 is solvent, 80 for water
c        1 is solute, 4  for protein
      enddo
c e+++++++++++++++                       
      write(20,*) "GRID SCALE=",scale
      do i=1,icount1b
         ! sequence of charged atoms and their grid index
         write(20,*) gchrg(i),gchrgp(1,i),gchrgp(2,i),gchrgp(3,i)
      enddo
      close(20)

      end
